var CalculateTime = require('../../../utils/util.js');
var remoteUrl = getApp().globalData.remoteurl;
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    label: [],
    loadingHidden: true,
    noneHidden: true,
    pageNumber: 1,
    praise:[],
    praiseNumber:-1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    wx: wx.request({
      // url: 'http://remotehost:8080/getHotFoodLabel',
      url: remoteUrl + 'getHotFoodLabel',
      method: 'GET',
      success: function (res) {
        
        that.setData({
          label: res.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    wx.showNavigationBarLoading();
    that.getFoodCards();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    that.setData({
      pageNumber: 1,
      noneHidden: true
    })
    wx: wx.showNavigationBarLoading();
    wx: wx.request({
      // url: 'http://localhost:8080/getHotFoodLabel',
      url: remoteUrl + 'getHotFoodLabel',
      method: 'GET',
      success: function (res) {
        that.setData({
          label: res.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    that.getFoodCards();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    that.setData({
      loadingHidden: false,
      pageNumber: that.data.pageNumber + 1
    })
    if (that.data.pageNumber > that.data.totalNumber) {
      that.setData({
        noneHidden: false,
        loadingHidden: true
      })
    } else {
      wx.request({
        // url: 'http://localhost:8080/getFoodCards',
        url: remoteUrl + 'getFoodCards',
        method: 'GET',
        data: { pageNumber: that.data.pageNumber },
        success: function (res) {
          var cards = that.data.cards;
          var texts = that.data.texts;
          for (var i = 0; i < res.data.list.length; i++) {
            var time_before = Date.parse(res.data.list[i][7]);
            var time = CalculateTime.formatTime(time_before);
            res.data.list[i][7] = time;
            cards.push(res.data.list[i]);
            var text = res.data.list[i][2].substr(0, 69);
            if (text.length == 69) {
              text += "..."
            }
            texts.push(text)
          }
          that.setData({
            cards: cards,
            loadingHidden: true,
            texts:texts
          })
        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  Praise: function (e) {
    var index = e.currentTarget.dataset.id;
    wx: wx.request({
      url: remoteUrl + "praiseFoodCard",
      data: {
        cardId: e.currentTarget.dataset.cardid
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        var num = parseInt(that.data.cards[index][5]) + 1
        that.setData({
          ['praise[' + index + ']']: true,
          ['cards[' +index +'][5]']: num,
        })
      },
    })
  },
  CancelPraise: function (e) {
    var index = e.currentTarget.dataset.id;
    wx: wx.request({
      url: remoteUrl + "cancelPraiseFoodCard",
      data: {
        cardId: e.currentTarget.dataset.cardid
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        var num = parseInt(that.data.cards[index][5]) - 1
        that.setData({
          ['praise[' + index + ']']: false,
          ['cards[' + index + '][5]']: num
        })
      },
    })
  },
  bindTopicMore: function () {
    wx.navigateTo({
      url: '../topic/topic',
      success: function () {
        console.log('跳转到topic页面成功')
      },
      fail: function () {
        console.log('跳转到topic页面失败')
      }
    })
  },
  bindBlog: function (e) {
    var id = e.currentTarget.id;
    var detailInfo = JSON.stringify(that.data.cards[id]);
    var encodeDetailInfo = encodeURIComponent(detailInfo);
    wx.navigateTo({
      url: '../blogdetail/blogdetail?detailInfo=' + encodeDetailInfo,
      success: function () {
        console.log('跳转到帖子详情页面成功')
      },
      fail: function () {
        console.log('跳转到帖子详情页面失败')
      }
    })
  },
  bindTopicDetail:function(e){
    var topic = e.currentTarget.id;
    var topic1 = topic.substring(1,topic.length);
    wx:wx.navigateTo({
      url: '../topicdetail/topicdetail?topicName=' + topic1 + "&type=food",
    })
  },
  getFoodCards:function(){
    wx: wx.request({
      // url: 'http://localhost:8080/getFoodCards',
      url: remoteUrl + 'getFoodCards',
      method: 'GET',
      data: { pageNumber: that.data.pageNumber },
      success: function (res) {
        that.setData({
          cards: res.data.list,
          totalNumber: res.data.size
        })
        for (var i = 0; i < res.data.list.length; i++) {
          var time_before = Date.parse(res.data.list[i][7]);
          var time = CalculateTime.formatTime(time_before);
          var texts = res.data.list[i][2].substr(0, 69);
          if (texts.length == 72) {
            texts += "..."
          }
          that.setData({
            ['cards[' + i + '][7]']: time,
            ['texts[' + i + ']']: texts
          })
        }
        wx.hideNavigationBarLoading();
      },
      fail:function(){
        wx:wx.showToast({
          title: '网络出错了',
          icon: 'none',
          duration: 1500,
        })
        wx: wx.hideNavigationBarLoading()
      }
    })
  }
})